package com.bingxue.edu.management.system.controller;

import com.bingxue.edu.management.system.model.entity.Resource;
import com.bingxue.edu.management.system.model.query.ResourceQuery;
import com.bingxue.edu.management.system.model.req.ResourceReq;
import com.bingxue.edu.management.system.model.req.ResourceSortReq;
import com.bingxue.edu.management.system.model.resp.ResourceResp;
import com.bingxue.edu.management.system.service.ResourceService;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.model.SortQuery;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 资源接口
 *
 * @author panbo
 * @since 2024/07/21 22:36
 */
@Tag(name = "资源接口")
@RestController
@RequestMapping("/system/resource")
@RequiredArgsConstructor
public class ResourceController {

    private final ResourceService resourceService;

    @Operation(summary="查询列表")
    @ApiResource("system:resource:manage")
    @GetMapping("/list")
    public Response<List<ResourceResp>> list(ResourceQuery query, SortQuery sortQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, sortQuery);
        List<ResourceResp> list = resourceService.listAs(queryWrapper, ResourceResp.class);
        return Response.success(list);
    }

    @Operation(summary="查询详情")
    @ApiResource("system:resource:manage")
    @GetMapping("/detail/{id}")
    public Response<ResourceResp> detail(@PathVariable String id) {
        ResourceResp detail = resourceService.getMapper().selectOneWithRelationsByIdAs(id, ResourceResp.class);
        return Response.success(detail);
    }

    @Operation(summary="创建数据")
    @ApiResource("system:resource:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated ResourceReq req) {
        Resource resource = resourceService.copy(req);
        resourceService.save(resource);
        return Response.success();
    }

    @Operation(summary="更新数据")
    @ApiResource("system:resource:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated ResourceReq req, @PathVariable String id) {
        Resource resource = resourceService.copyById(req, id);
        resourceService.updateById(resource);
        return Response.success();
    }

    @Operation(summary="删除数据")
    @ApiResource("system:resource:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        resourceService.removeByIds(ids);
        return Response.success();
    }

    @Operation(summary="排序数据")
    @ApiResource("system:resource:manage")
    @PostMapping("/sort")
    public Response<Void> sort(ResourceSortReq req) {
        resourceService.sortResources(req);
        return Response.success();
    }

}
